package com.rd.student.ruanjian.class4.lichunquan.Text02;

import java.util.Scanner;

public class Test12 {
    //五子棋
    public static void main(String[] args) {
        FiveInARow f = new FiveInARow();
        Scanner sc = new Scanner(System.in);
        int i =0 ;
        int x,y;
        String put;
        f.printOfCheckerBoard();
        while(true){
            if(i % 2 == 0){
                //黑子下
                System.out.println("黑子下");
                System.out.println("输入坐标x,y");
                x = sc.nextInt();
                y = sc.nextInt();
                put = "⚫";
                if(!f.input(put,x,y)){
                    System.out.println("黑子胜利");
                    break;
                }
            }else{
                //白子下
                System.out.println("白子下");
                System.out.println("输入坐标x,y");
                x = sc.nextInt();
                y = sc.nextInt();
                put = "⚪";
                System.out.println("输入坐标x,y");
                if(!f.input(put,x,y)){
                    System.out.println("白子胜");
                    break;
                }


            }
            i++;
        }
        f.printOfCheckerBoard();

    }
}
class FiveInARow{
    Scanner sc = new Scanner(System.in);
    private String[][] checkerboard ={
            {"0","0","0","0","0","0","0","0","0","0"},
            {"0","0","0","0","0","0","0","0","0","0"},
            {"0","0","0","0","0","0","0","0","0","0"},
            {"0","0","0","0","0","0","0","0","0","0"},
            {"0","0","0","0","0","0","0","0","0","0"},
            {"0","0","0","0","0","0","0","0","0","0"},
            {"0","0","0","0","0","0","0","0","0","0"},
            {"0","0","0","0","0","0","0","0","0","0"},
            {"0","0","0","0","0","0","0","0","0","0"},
            {"0","0","0","0","0","0","0","0","0","0"}
    };
    public boolean input(String str ,int x ,int y){
        //输入判断
        if(x > checkerboard[y].length-1 || y > checkerboard.length-1){
            System.out.println("下在了无效位置");
            System.out.println("重新输入");
            x = sc.nextInt();
            y = sc.nextInt();
            input(str,x,y);
        }
        if(!checkerboard[y][x].equals("0")){
            System.out.println("该位置已经下过了！");
            System.out.println("重新输入");
            x = sc.nextInt();
            y = sc.nextInt();
            input(str,x,y);
        }else{
            checkerboard[y][x] = str;
            return judgeLayer(x,y,str);
        }
        return judgeLayer(x,y,str);
    }
    public void printOfCheckerBoard(){

        System.out.println("================================================================");
        for (int i = 0; i < checkerboard.length; i++) {
            System.out.println();
            System.out.print("||");
            for (int j = 0; j < checkerboard.length; j++) {
                System.out.print(" "+ checkerboard[i][j] + " ");
            }
            System.out.print("||");
            System.out.println();
        }
        System.out.println("================================================================");
    }
    public boolean judgeLayer(int x , int  y , String str) {
        boolean b = true;
        b = x(x,y,checkerboard,str);
        if(b){
            b = y(x,y,checkerboard,str);
        }
        if(b){
            b = xy(x,y,checkerboard,str);
        }
        return b;
    }
    //纵判断
    public static boolean x(int x , int y ,String[][] checkerboard,String str){
        int count = 0;
        for (int y1 = y ; y1 < checkerboard.length  ; y1++) {
            if (checkerboard[y1][x].equals(str)){
                count++;
                if(count == 5)return false;
            }
        }
        if(count>0)count-- ;
        for (int y2 = y ; y2 >0  ; y2--) {
            if (checkerboard[y2][x].equals(str)){
                count++;
                if(count == 5)return false;
            }
        }
        return true;
    }
    //横判断
    public static boolean y(int x , int y ,String [][] checkerboard,String str){
        int count = 0;
        for (int x1 = x; x1 < checkerboard[y].length  ; x1++) {
            if (checkerboard[y][x1].equals(str)){
                count++;
                if(count == 5)return false;
            }
        }
        if(count>0)count-- ;

        for (int x2 = x; x2 > 0  ; x2--) {
            if (checkerboard[y][x2].equals(str)){
                count++;
                if(count == 5)return false;
            }
        }
        return true;

    }
    //斜判断
    public static boolean xy(int x , int y ,String [][] checkerboard,String str){
        int count = 0;
        for (int x1 = x ,y1 = y; x1 < checkerboard[y].length && y1< checkerboard.length ; x1++ ,y1++) {
            if (checkerboard[y1][x1].equals(str)){
                count++;
                if(count == 5)return false;
            }
        }
        if(count>0)count-- ;
        for (int x2 = x,y2 = y; x2>0 && y2>0 ; x2--,y2--) {
            if (checkerboard[y2][x2].equals(str)){
                count++;
                if(count == 5)return false;
            }
        }
        return true;
    }
}
